#encoding: utf-8
class DataController < ApplicationController
  def create
    json_str = request.body.read
    json = JSON.parse(json_str)
    failure_ary =[]
    json["data"].each { |item|
      begin
        User.transaction do
          bike = Bike.find_by(out_id: item["id"])
          #如果该数据已经同步,更新原有数据
          if bike.present?
            #更新用户的手机号
            user = User.find_by(mobile: item["mobile"])
            if user.present?
              user.update(name: item["name"], gender: item["gender"], address: item["address"], idCard: item["idCard"], career: item["career"], company: item["company"])
            else
              user = User.create(mobile: item["mobile"], name: item["name"], gender: item["gender"], address: item["address"], idCard: item["idCard"], career: item["career"], company: item["company"])
            end
            bike.update(
                user_id: user.id,
                licenseNum: item["licenseNum"], antiTheftCode: item["antiTheftCode"], recordDate: item["recordDate"],
                prop_attributes: {
                    id: bike.prop.id,
                    brand: item["brand"],
                    vin: item["vin"],
                    insurance: item["insurance"],
                    color: item["color"],
                    purchaseDate: item["purchaseDate"],
                    purchasePrice: item["purchasePrice"],
                    newBike: item["newBike"],
                    insuranceNum: item["insuranceNum"],
                    industry: item["industry"],
                    motorNum: item["motorNum"],
                    promise: item["promise"],
                    modelNum: item["modelNum"],
                    site: item["site"],
                    city: item["city"]}
            )
          else
            user = User.find_by(mobile: item["mobile"])
            if user.present?
              user.update(name: item["name"], gender: item["gender"], address: item["address"], idCard: item["idCard"], career: item["career"], company: item["company"])
            else
              user = User.create(mobile: item["mobile"], name: item["name"], gender: item["gender"], address: item["address"], idCard: item["idCard"], career: item["career"], company: item["company"])
            end
            user.bikes.create(
                out_id: item["id"], licenseNum: item["licenseNum"], antiTheftCode: item["antiTheftCode"], recordDate: item["recordDate"],
                prop_attributes: {
                    brand: item["brand"],
                    vin: item["vin"],
                    insurance: item["insurance"],
                    color: item["color"],
                    purchaseDate: item["purchaseDate"],
                    purchasePrice: item["purchasePrice"],
                    newBike: item["newBike"],
                    insuranceNum: item["insuranceNum"],
                    industry: item["industry"],
                    motorNum: item["motorNum"],
                    promise: item["promise"],
                    modelNum: item["modelNum"],
                    site: item["site"],
                    city: item["city"]})
          end
        end
      rescue Exception
        failure_ary << item["id"]
      end
    }
    render json: Success.new(id: failure_ary)
  end

  def update
    json_str = request.body.read
    json = JSON.parse(json_str)
    failure_ary =[]
    json["data"].each { |item|
      begin
        User.transaction do
          bike = Bike.find_by(out_id: item["id"])
          #如果该数据已经同步,更新原有数据
          if bike.present?

            #更新用户的手机号
            user = User.find_by(mobile: item["mobile"])
            if user.present?
              user.update(name: item["name"], gender: item["gender"], address: item["address"], idCard: item["idCard"], career: item["career"], company: item["company"])
            else
              user = User.create(mobile: item["mobile"], name: item["name"], gender: item["gender"], address: item["address"], idCard: item["idCard"], career: item["career"], company: item["company"])
            end
            bike.update(
                user_id: user.id,
                licenseNum: item["licenseNum"], antiTheftCode: item["antiTheftCode"], recordDate: item["recordDate"],
                prop_attributes: {
                    id: bike.prop.id,
                    brand: item["brand"],
                    vin: item["vin"],
                    insurance: item["insurance"],
                    color: item["color"],
                    purchaseDate: item["purchaseDate"],
                    purchasePrice: item["purchasePrice"],
                    newBike: item["newBike"],
                    insuranceNum: item["insuranceNum"],
                    industry: item["industry"],
                    motorNum: item["motorNum"],
                    promise: item["promise"],
                    modelNum: item["modelNum"],
                    site: item["site"],
                    city: item["city"]}
            )
          else
            user = User.find_by(mobile: item["mobile"])
            if user.present?
              user.update(name: item["name"], gender: item["gender"], address: item["address"], idCard: item["idCard"], career: item["career"], company: item["company"])
            else
              user = User.create(mobile: item["mobile"], name: item["name"], gender: item["gender"], address: item["address"], idCard: item["idCard"], career: item["career"], company: item["company"])
            end
            user.bikes.create(
                out_id: item["id"], licenseNum: item["licenseNum"], antiTheftCode: item["antiTheftCode"], recordDate: item["recordDate"],
                prop_attributes: {
                    brand: item["brand"],
                    vin: item["vin"],
                    insurance: item["insurance"],
                    color: item["color"],
                    purchaseDate: item["purchaseDate"],
                    purchasePrice: item["purchasePrice"],
                    newBike: item["newBike"],
                    insuranceNum: item["insuranceNum"],
                    industry: item["industry"],
                    motorNum: item["motorNum"],
                    promise: item["promise"],
                    modelNum: item["modelNum"],
                    site: item["site"],
                    city: item["city"]})
          end
        end
      rescue Exception
        failure_ary << item["id"]
      end
    }
    render json: Success.new(id: failure_ary)
  end

  def delete
    json_str = request.body.read
    json = JSON.parse(json_str)
    ids = json["id"]
    failure_ids = []
    ids.each { |id|
      bike = Bike.find_by(out_id: id)
      if bike.blank?
        failure_ids << id
      else
        failure_ids << id unless bike.destroy
      end
    }
    render json: Success.new(id: failure_ids)
  end
end
